package com.dq.Controller;


import com.dq.Pojo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/avatar")
public class FileUploadController {

    @Value("${upload.path}")
    private String filepath;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        File descfile=new File(filepath,filename);
        file.transferTo(descfile);
        String url="http://localhost:8080/static/upload/"+filename;
        return Result.success(url);
    }
}
